package com.aku.security;

import com.aku.mapper.UserMapper;
import org.springframework.security.core.userdetails.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserAuthService implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.aku.pojo.User user = userMapper.selectUserByName(username);
        if (user.getPassword() == null) {
            throw new UsernameNotFoundException("登录失败，用户名或密码错误");
        }
        return User.withUsername(username)
                .password(user.getPassword())
                .roles(user.getRole())
                .build();
    }
}
